package org.kabeja.parser.entities;

import org.kabeja.dxf.entity.DXFEntity;
import org.kabeja.dxf.entity.DXFSpline;
import org.kabeja.dxf.helpers.SplinePoint;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 11:27
 */
public class DXFSplineHandler extends AbstractEntityHandler {
    public static final int CONTROL_POINT_X = 10;
    public static final int CONTROL_POINT_Y = 20;
    public static final int CONTROL_POINT_Z = 30;
    public static final int FIT_POINT_X = 11;
    public static final int FIT_POINT_Y = 21;
    public static final int FIT_POINT_Z = 31;
    public static final int START_TANGENT_X = 12;
    public static final int START_TANGENT_Y = 22;
    public static final int START_TANGENT_Z = 32;
    public static final int END_TANGENT_X = 13;
    public static final int END_TANGENT_Y = 23;
    public static final int END_TANGENT_Z = 33;
    public static final int FIT_TOLERANCE = 44;
    public static final int KNOTS = 40;
    public static final int WEIGHTS = 41;
    public static final int CONTROLPOINT_TOLERANCE = 42;
    public static final int KNOT_TOLERANCE = 43;
    public static final int NUMBER_OF_FIT_POINTS = 74;
    public static final int NUMBER_OF_CONTROL_POINTS = 73;
    public static final int NUMBER_OF_CONTROL_POINTS2 = 96;
    public static final int NUMBER_OF_NODES = 72;
    public static final int NUMBER_OF_NODES2 = 95;
    public static final int DEGREE = 71;
    private DXFSpline spline;
    private SplinePoint point;
    private double[] knots;
    private double[] weights;
    private int knotsCount;
    private int controlPointCount;

    public DXFSplineHandler() {
    }

    @Override
    public String getDXFEntityName() {
        return "SPLINE";
    }

    @Override
    public void endDXFEntity() {
        this.spline.setKnots(this.knots);
        this.spline.setWeights(this.weights);
    }

    @Override
    public DXFEntity getDXFEntity() {
        return this.spline;
    }

    @Override
    public boolean isFollowSequence() {
        return false;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case 10:
                this.point = new SplinePoint();
                this.point.setType(0);
                this.point.setX(var2.getDoubleValue());
                this.spline.addSplinePoint(this.point);
                break;
            case 11:
                this.point = new SplinePoint();
                this.point.setType(1);
                this.point.setX(var2.getDoubleValue());
                this.spline.addSplinePoint(this.point);
                break;
            case 12:
                this.point = new SplinePoint();
                this.point.setType(2);
                this.point.setX(var2.getDoubleValue());
                this.spline.addSplinePoint(this.point);
                break;
            case 13:
                this.point = new SplinePoint();
                this.point.setType(3);
                this.point.setX(var2.getDoubleValue());
                this.spline.addSplinePoint(this.point);
                break;
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 19:
            case 24:
            case 25:
            case 26:
            case 27:
            case 28:
            case 29:
            case 34:
            case 35:
            case 36:
            case 37:
            case 38:
            case 39:
            case 45:
            case 46:
            case 47:
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
            case 58:
            case 59:
            case 60:
            case 61:
            case 62:
            case 63:
            case 64:
            case 65:
            case 66:
            case 67:
            case 68:
            case 69:
            case 70:
            case 75:
            case 76:
            case 77:
            case 78:
            case 79:
            case 80:
            case 81:
            case 82:
            case 83:
            case 84:
            case 85:
            case 86:
            case 87:
            case 88:
            case 89:
            case 90:
            case 91:
            case 92:
            case 93:
            case 94:
            default:
                super.parseCommonProperty(var1, var2, this.spline);
                break;
            case 20:
                this.point.setY(var2.getDoubleValue());
                break;
            case 21:
                this.point.setY(var2.getDoubleValue());
                break;
            case 22:
                this.point.setY(var2.getDoubleValue());
                break;
            case 23:
                this.point.setY(var2.getDoubleValue());
                break;
            case 30:
                this.point.setZ(var2.getDoubleValue());
                break;
            case 31:
                this.point.setZ(var2.getDoubleValue());
                break;
            case 32:
                this.point.setZ(var2.getDoubleValue());
                break;
            case 33:
                this.point.setZ(var2.getDoubleValue());
                break;
            case 40:
                this.knots[this.knotsCount] = var2.getDoubleValue();
                ++this.knotsCount;
                break;
            case 41:
                this.weights[this.controlPointCount] = var2.getDoubleValue();
                ++this.controlPointCount;
                break;
            case 42:
                this.spline.setControlPointTolerance(var2.getDoubleValue());
                break;
            case 43:
                this.spline.setKnotsTolerance(var2.getDoubleValue());
                break;
            case 44:
                this.spline.setFitTolerance(var2.getDoubleValue());
                break;
            case 71:
                this.spline.setDegree(var2.getIntegerValue());
                break;
            case 72:
                this.knots = new double[var2.getIntegerValue()];
                this.knotsCount = 0;
                this.spline.setNodePointsSize(var2.getIntegerValue());
                break;
            case 73:
                this.weights = new double[var2.getIntegerValue()];
                this.controlPointCount = 0;
                this.spline.setControlPointSize(var2.getIntegerValue());
                break;
            case 74:
                this.spline.setFitPointSize(var2.getIntegerValue());
                break;
            case 95:
                this.knots = new double[var2.getIntegerValue()];
                this.knotsCount = 0;
                this.spline.setNodePointsSize(var2.getIntegerValue());
        }

    }

    @Override
    public void parseObject(int var1, DXFValue var2, int type) {
        super.parseObjectProperty(var1, var2, type, this.spline);
    }

    @Override
    public void startDXFEntity() {
        this.spline = new DXFSpline();
    }
}
